<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>date/getDate.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="color.html">color</a><ul class='methods'><li data-type='method'><a href="color.html#.setColorOpacity">setColorOpacity</a></li></ul></li><li><a href="data.html">data</a><ul class='methods'><li data-type='method'><a href="data.html#.getMax">getMax</a></li><li data-type='method'><a href="data.html#.getMin">getMin</a></li><li data-type='method'><a href="data.html#.getTotal">getTotal</a></li><li data-type='method'><a href="data.html#.toArray">toArray</a></li><li data-type='method'><a href="data.html#.toJson">toJson</a></li></ul></li><li><a href="date.html">date</a><ul class='methods'><li data-type='method'><a href="date.html#.dateFormat">dateFormat</a></li><li data-type='method'><a href="date.html#.getDate">getDate</a></li></ul></li><li><a href="is.html">is</a><ul class='methods'><li data-type='method'><a href="is.html#.isIDCard">isIDCard</a></li><li data-type='method'><a href="is.html#.isPhone">isPhone</a></li><li data-type='method'><a href="is.html#.isType">isType</a></li></ul></li></ul>
</nav>

<div id="main">
    
    <h1 class="page-title">date/getDate.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * 将 Date 格式的日期格式化为字符串
 * 
 * @memberof date
 * 
 * @param {Date} date 日期(必传)
 * 
 * @param {String} fmt 字符串格式（可选）
 * &lt;br /> 默认为 yyyy-MM-dd hh:mm:ss 
 * &lt;br /> 可通过 fmt 获取年月日，如传入 MM 获取日期月份 
 * 
 * @returns 日期字符串
 * 
 * @example  
 * dateFormat(new Date()); // 2021-02-01 10:08:38
 * dateFormat(new Date(),'MM'); // 02
 */
function dateFormat(date, fmt) {
    const {isType} = this;
    if(!isType(date,'Date')){
        return 'date needs to be Date()'
    }
    fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            const str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
        }
    }
    return fmt
}
/**
 * 计算一个基于指定日期的新日期
 * 
 * @memberof date
 * 
 * @param {Number} hour 小时(必传)
 * &lt;br /> 要计算的新日期和指定日期的小时差
 * &lt;br /> + 代表以后的日期，- 代表以前的日期
 * &lt;br /> 默认为0
 * 
 * @param {Date} base 指定日期（可选）
 * &lt;br /> 默认为 new Date()
 * 
 * @returns Date 格式的日期
 * 
 * @example  
 * getDate(-2); // 2021-02-01T04:41:44.781Z
 * getDate(24 * 3, new Date('2020-10-10')); // 2020-10-13T00:00:00.000Z
 */
function getDate(hour, base) {
    hour = hour || 0
    base = base || new Date()
    const {isType} = this;
    if(!isType(base,'Date')){
        return 'base needs to be Date()'
    }
    const baseTime  = base.getTime()
    const newTime =  baseTime + hour * (3600 * 1000)
    return new Date(newTime)
}
module.exports.dateFormat = dateFormat
module.exports.getDate = getDate
</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> on Wed Feb 24 2021 16:40:34 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
